﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SmartSpace.Core.Metadata;
using SmartSpace.Data;
using SmartSpace.Core;

namespace SmartSpace.Workflow
{
    public class ActivityMetadata
    {
        public Guid Id { get; set; }
        public string Caption { get; set; }
        public string Type { get; set; }
        public int Depth { get; set; }
        public bool IsRoot { get; set; }

        public WorkflowMetadata Workflow { get; set; }
        public Guid WorkflowId { get; set; }

        public List<EventMetadata> Events { get; set; }

        /// <summary>
        /// 当前活动执行者的权限
        /// </summary>
        public List<ActivityRight> Rights { get; set; }

        public ActivityMetadata()
        {
        }

        public bool CanVisit(FieldMetadata field, List<Role> roles)
        {
            foreach (ActivityRight right in Rights)
            {
                foreach (Role role in roles)
                {
                    if (role.Id == right.RoleId)
                    {
                        foreach (FieldRight fieldRight in right.FieldRights)
                        {
                            if (fieldRight.Property.Id == field.PropertyId)
                            {
                                if (fieldRight.IsAllowVisit)
                                {
                                    return true;
                                }
                            }
                        }
                    }
                }
            }
            return false;
        }

        public bool CanModify(FieldMetadata field, List<Role> roles)
        {
            foreach (ActivityRight right in Rights)
            {
                foreach (Role role in roles)
                {
                    if (role.Id == right.RoleId)
                    {
                        foreach (FieldRight fieldRight in right.FieldRights)
                        {
                            if (fieldRight.Property.Id == field.PropertyId)
                            {
                                if(fieldRight.IsAllowModify)
                                {
                                    return true;
                                }
                            }
                        }
                    }
                }
            }
            return false;
        }
    }
}
